//#define _CRT_SECURE_NO_WARNINGS 1
//
//#include"Queue.h"
//
//void Test()
//{
//	Queue q;
//	QueueInit(&q);
//	QueuePush(&q, 1);
//
//	QueuePush(&q, 2);
//
//	QueuePush(&q, 3);
//
//	QueuePush(&q, 4);
//
//
//	while (!QueueEmpty(&q))
//	{
//		printf("%d ", QueueFront(&q));
//		QueuePop(&q);
//	}
//	QueueDestroy(&q);
//}
//int main()
//{
//	Test();
//	return 0;
//}

#include<stdio.h>
#include<assert.h>
#include<stdbool.h>
#include<stdlib.h>

typedef struct {
    int* a;
    int front;
    int rear;
    int k;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    int *tmp = (int*)malloc((k + 1) * sizeof(int));
    obj->a = tmp;
    obj->front = 0;
    obj->rear = 0;
    obj->k = k;
    return obj;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->front == obj->rear;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return obj->front == (obj->rear + 1 + obj->k + 1) % (obj->k + 1);
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj))
        return false;
    obj->a[obj->rear] = value;
    obj->rear++;
    obj->rear %= obj->k + 1;
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
        return false;
    obj->front++;
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    return obj->a[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj) {

    //return obj->a[(obj->rear - 1 + obj->k + 1) % (obj->k + 1)];

    return obj->a[(obj->rear - 1 + obj->k + 1) % (obj->k + 1)];
}



void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->a);
    free(obj);
    obj->a = NULL;
    obj = NULL;
}

int main()
{
    MyCircularQueue* obj = myCircularQueueCreate(3);
    myCircularQueueEnQueue(obj->a, 1);
    myCircularQueueEnQueue(obj->a, 2);
    myCircularQueueEnQueue(obj->a, 3);
    myCircularQueueEnQueue(obj->a, 4);

    return 0;
}